package com.youlian.cloud.user.service.impl;

import com.youlian.cloud.core.response.ListResponseModel;
import com.youlian.cloud.core.response.PageInfoModel;
import com.youlian.cloud.core.response.PageResponseModel;
import com.youlian.cloud.core.response.ResponseCommonModel;
import com.youlian.cloud.core.bean.ExceptionHandle;
import com.youlian.cloud.core.bean.PageModel;
import com.youlian.cloud.core.util.DateUtils;
import com.youlian.cloud.core.util.ObjectUtils;
import com.youlian.cloud.spring.common.utils.BeanUtils;
import com.youlian.cloud.user.dao.IMenusDao;
import com.youlian.cloud.user.dao.entity.MenusEntity;
import com.youlian.cloud.user.service.IMenusService;
import com.youlian.cloud.user.service.request.AddMenusRequest;
import com.youlian.cloud.user.service.request.ModifyMenusRequest;
import com.youlian.cloud.user.service.request.QueryMenusRequest;
import com.youlian.cloud.user.service.response.MenusResponse;
import com.youlian.cloud.user.service.response.MenusTreeResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: tangy
 * @Date: 2018/11/14 15:26
 * @email tangy@elab-plus.com
 * @Description:
 */
@RestController
@RequestMapping("/menus")
@Api(value = "菜单管理", description = "菜单管理", tags = {"菜单管理"})
public class MenusServiceImpl implements IMenusService {

    @Autowired
    private IMenusDao menusDaoImpl;

    @Override
    @Transactional
    @PostMapping(value = "/add", produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "添加菜单", notes = "添加菜单", nickname = "tangy")
    @ExceptionHandle(username = "tangy", ModuleName = MenusServiceImpl.class)
    public ResponseCommonModel addMenus(@RequestBody @Valid AddMenusRequest request) throws Exception {
        ResponseCommonModel response = new ResponseCommonModel();
        MenusEntity entity = new MenusEntity();
        BeanUtils.copyProperties(entity, request);
        entity.setCreated(DateUtils.getCurrentDateTime());
        entity.setStatus(1);
        int menuId = menusDaoImpl.insert(entity);
        if(menuId <= 0){
            response.setSuccess(false);
            response.setErrorCode("");
            response.setMessage("");
            return response;
        }
        response.setId(String.valueOf(menuId));
        return response;
    }

    @Override
    @Transactional
    @PostMapping(value = "/modify", produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "修改菜单", notes = "修改菜单", nickname = "tangy")
    @ExceptionHandle(username = "tangy", ModuleName = MenusServiceImpl.class)
    public ResponseCommonModel modifyMenus(@RequestBody @Valid ModifyMenusRequest request) throws Exception {
        ResponseCommonModel response = new ResponseCommonModel();
        MenusEntity entity = new MenusEntity();
        BeanUtils.copyProperties(entity, request);
        entity.setUpdated(DateUtils.getCurrentDateTime());
        int menuId = menusDaoImpl.updateById(entity);
        int adf = 1 / 0;
        if(menuId <= 0){
            response.setSuccess(false);
            response.setErrorCode("");
            response.setMessage("");
            return response;
        }
        response.setId(String.valueOf(menuId));
        return response;
    }

    @Override
    @PostMapping(value = "/query", produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "查询菜单", notes = "查询菜单", nickname = "tangy")
    @ExceptionHandle(username = "tangy", ModuleName = MenusServiceImpl.class)
    public PageResponseModel<MenusResponse> queryMenus(@RequestBody QueryMenusRequest request) throws Exception {
        int pageNo = request.getPageNo();
        int pageSize = request.getPageSize();
        PageResponseModel response = new PageResponseModel();
        MenusEntity entity = new MenusEntity();
        entity.setAuthCode(request.getAuthCode());
        if(ObjectUtils.isNotEmpty(request.getAuthName())){
            entity.setAuthName("%"+request.getAuthName()+"%");
        }
        PageModel<MenusEntity> page = new PageModel<>(0, pageNo, pageSize);
        page = menusDaoImpl.queryMenus(page, entity);
        PageInfoModel<MenusResponse> pageInfoModel = new PageInfoModel<>();
        BeanUtils.copyProperties(pageInfoModel,page);
        response.setPageModel(pageInfoModel);
        return response;
    }

    @Override
    @PostMapping(value = "/queryMenusTree", produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "查询菜单树结构", notes = "查询菜单树结构", nickname = "tangy")
    @ExceptionHandle(username = "tangy", ModuleName = MenusServiceImpl.class)
    public ListResponseModel<MenusTreeResponse> queryMenusTree(@RequestBody QueryMenusRequest request) throws Exception {
        ListResponseModel response = new ListResponseModel();
        int level = request.getLeavl();
        String chkStyle = request.getChkStyle();
        MenusEntity entity = new MenusEntity();
        entity.setStatus(1);
        List<MenusEntity> menusList = menusDaoImpl.queryMenusTree(entity);
        List<MenusTreeResponse> menusTree = new ArrayList<>();
        if(menusList != null && menusList.size() > 0){
            for(MenusEntity m: menusList){
                MenusTreeResponse treeItem = new MenusTreeResponse();
                treeItem.setId(m.getId());
                treeItem.setName(m.getAuthName());
                treeItem.setAuthCode(m.getAuthCode());
                treeItem.setpId(m.getParentId());
                if("radio".equals(chkStyle)){
                    if(level == 1){
                        treeItem.setChkDisabled(true);
                    }
                    if(level != 1 && m.getLevel() != (level -1)){
                        treeItem.setChkDisabled(true);
                    }
                }
                treeItem.setOpen(false);
                menusTree.add(treeItem);
            }
        }
        response.setList(menusTree);
        return response;
    }
}
